package com.imau.controller;

import com.alibaba.fastjson.JSON;
import com.imau.bean.Brand;
import com.imau.service.BrandService;
import com.imau.service.impl.BrandServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "BrandAddOrUpdateServlet", value = "/BrandAddOrUpdateServlet")
public class BrandAddOrUpdateServlet extends HttpServlet {

    private BrandService service = new BrandServiceImpl();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        this.doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.setCharacterEncoding("utf-8");

        //1.获取JSON串
        String jsonString = request.getReader().readLine();
        //2.转成java对象
        Brand brand = JSON.parseObject(jsonString, Brand.class);

        System.out.println(brand);
        //3.调用服务添加
        boolean flag;
        if(brand.getId()!=null)
        {
            flag= service.edit(brand);
        }else {
            flag= service.add(brand);
        }


        //4.响应结果
        response.getWriter().write("OK");
    }
}
